# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html
import pymysql


class ChengduPipeline(object):

    def __init__(self):
        dbparams = {
            'host': '127.0.0.1',
            'port': 3306,
            'user': 'root',
            'password': 'root',
            'database': 'lianjia',
            'charset': 'utf8'
        }
        self.conn = pymysql.connect(**dbparams)
        self.cursor = self.conn.cursor()
        self._sql = None

    def process_item(self, item, spider):
        print('存')
        self.cursor.execute(self.sql, (item['title'], item['region'], item['address'], item['housing_estate'],
                                       item['total_price'], item['unit_price'],item['room_type'],item['room_size'],
                                       item['floor'],item['direction'],item['house_type'],item['created_date'],
                                       item['house_info'],item['transactions'],item['hang_out_date'],item['url']))
        self.conn.commit()
        print('存')
        return item

    @property
    def sql(self):
        if not self._sql:
            self._sql = "insert into chengdu(title,region,address,housing_estate,total_price,unit_price,room_type," \
                        "room_size,floor,direction,house_type,created_date,house_info,transactions,hang_out_date,url) " \
                        "values(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"
            return self._sql
        return self._sql
